package com.sdry.web.controller.zc;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.model.zc.ZcMaterielAndTrayEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.zc.ZcBindAndUnbindService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcBindAndUnbindController   
 * @Description:  物料与托盘的绑定与解绑
 * @Author:       zc   
 * @CreateDate:   2019年6月3日 上午9:22:20   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("bindAndUnbind")
public class ZcBindAndUnbindController {

	@Resource
	private ZcBindAndUnbindService zcBindAndUnbindService;
	/**
	 * 进入绑定与解绑页面
	 * @return
	 */
	@RequestMapping("enterBindAndUnbindPage")
	public ModelAndView enterBindAndUnbindPage(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/zc/warehouse/bindAndUnbind");
		return mav;
	}
	/**
	 * 绑定物料托盘
	 * @param httpSession
	 * @param response
	 * @param zcMaterielAndTrayEntity
	 */
	@RequestMapping("bindingMaterielAndTray")
	public void bindingMaterielAndTray(HttpSession httpSession,HttpServletResponse response,ZcMaterielAndTrayEntity zcMaterielAndTrayEntity){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ZcSysUserEntity su = (ZcSysUserEntity)httpSession.getAttribute("user");
		zcMaterielAndTrayEntity.setBinding_person(su.getId());
		zcMaterielAndTrayEntity.setBinding_date(sdf.format(date));
		int affact = zcBindAndUnbindService.bindingMaterielAndTray(zcMaterielAndTrayEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 解绑物料托盘
	 * @param httpSession
	 * @param response
	 * @param zcMaterielAndTrayEntity
	 */
	@RequestMapping("unbindMaterielAndTray")
	public void unbindMaterielAndTray(HttpSession httpSession,HttpServletResponse response,ZcMaterielAndTrayEntity zcMaterielAndTrayEntity){
		int affact = zcBindAndUnbindService.unbindMaterielAndTray(zcMaterielAndTrayEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*******************************************APP************************************************/
	/**
	 * 绑定物料托盘
	 * @param response
	 * @param packType 包装类型0大箱1小箱
	 * @param materiel_num	产品码	
	 * @param sizeAndPre	规格/型号	
	 * @param mBatch	批次
	 * @param materiel_code 物料精确码，多个用逗号隔开
	 * @param tray_code	托盘码
	 * @param binding_person 绑定人id
	 */
	@RequestMapping("bindingMaterielAndTrayApp")
	public void bindingMaterielAndTrayApp(HttpServletResponse response,String packType,String materiel_num,String sizeAndPre,String mBatch,String materiel_code,String tray_code,String binding_person){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ZcMaterielAndTrayEntity zcMaterielAndTrayEntity = new ZcMaterielAndTrayEntity();
		zcMaterielAndTrayEntity.setPackType(Byte.parseByte(packType));
		zcMaterielAndTrayEntity.setMateriel_num(materiel_num);
		zcMaterielAndTrayEntity.setSizeAndPre(sizeAndPre);
		zcMaterielAndTrayEntity.setmBatch(mBatch);
		zcMaterielAndTrayEntity.setMateriel_code(materiel_code);
		zcMaterielAndTrayEntity.setTray_code(tray_code);
		zcMaterielAndTrayEntity.setBinding_person(Long.parseLong(binding_person));
		zcMaterielAndTrayEntity.setBinding_date(sdf.format(date));
		int affact = zcBindAndUnbindService.bindingMaterielAndTray(zcMaterielAndTrayEntity);
		JSONObject jsonObject = new JSONObject();
		if(affact > 0){
            jsonObject.put("status", "1");
            jsonObject.put("data", "提交成功");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else if(affact == 2) {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "提交失败,请绑定同一种物料");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }else {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "提交失败");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
	/**
	 * 解绑物料托盘
	 * @param response
	 * @param tray_code 托盘条码
	 * @param materiel_code 物料条码
	 */
	@RequestMapping("unbindMaterielAndTrayApp")
	public void unbindMaterielAndTrayApp(HttpServletResponse response,String tray_code,String materiel_code){
		ZcMaterielAndTrayEntity zcMaterielAndTrayEntity = new ZcMaterielAndTrayEntity();
		zcMaterielAndTrayEntity.setMateriel_code(materiel_code);
		zcMaterielAndTrayEntity.setTray_code(tray_code);
		int affact = zcBindAndUnbindService.unbindMaterielAndTray(zcMaterielAndTrayEntity);
		JSONObject jsonObject = new JSONObject();
		if(affact > 0){
            jsonObject.put("status", "1");
            jsonObject.put("data", "提交成功");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "提交失败");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
}
